package DatabasePackage;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import BusinessLogic.Session;

/**
 * This is the schema for user
 */
public class SessionDBTable extends DBtable {
  public enum SessionDBTScheme {
    COURSE_NAME("varchar(255)"), DAY("varchar(10)"), START_TIME("varchar(10)"), END_TIME("varchar(10)");
    private final String COLUMN_TYPE;
    
    SessionDBTScheme(String type) {
      COLUMN_TYPE = type;
    }
    
    String getType() {
      return COLUMN_TYPE;
    }
  }
  
  public SessionDBTable() {
    super(new HashMap<String, String>(), new LinkedList<String>(), new LinkedList<String>());
    keys.add(SessionDBTScheme.COURSE_NAME.toString());
    keys.add(SessionDBTScheme.DAY.toString());
    keys.add(SessionDBTScheme.END_TIME.toString());
    keys.add(SessionDBTScheme.START_TIME.toString());
    // Initializing the scheme
    for (SessionDBTScheme column : SessionDBTScheme.values())
      scheme.put(column.toString(), column.getType());
  }
  
  /**
   * @param username
   *          the name of the user you wish to get the details of
   * @return returns a list of the fields of the selected user if it exists, if
   *         he doesn't throws an exception
   * @throws SQLException
   *           if there was problem connecting to the sql database or there was
   *           a database error
   * @throws ClassNotFoundException
   *           if connector/j was not linked
   * @throws ItemNotFoundException
   *           if there is no user with the given username
   */
  public List<Session> getSessions(Connection con, String course) throws SQLException, ClassNotFoundException {
    Map<String, byte[]> conditions = new HashMap<String, byte[]>();
    conditions.put(SessionDBTScheme.COURSE_NAME.toString(), course.getBytes());
    LinkedList<Map<String, byte[]>> sessionsMap = (LinkedList<Map<String, byte[]>>) getItem(con,conditions);
    LinkedList<Session> sessions = new LinkedList<Session>();
    for(Map<String, byte[]> s: sessionsMap){
      sessions.add(createSession(s));
    }
    return sessions;
  }
  
  private Session createSession(Map<String, byte[]> s){
    String course = new String(s.get(SessionDBTScheme.COURSE_NAME.toString()));
    String day = new String(s.get(SessionDBTScheme.DAY.toString()));
    int start = Integer.parseInt(new String(s.get(SessionDBTScheme.START_TIME.toString())));
    int end = Integer.parseInt(new String(s.get(SessionDBTScheme.END_TIME.toString())));
    return new Session(course, day, start, end);
  }
  
  /**
   * Deletes the given user from the database, if he doesn't exist, throws an
   * exception
   * 
   * @param name
   *          the username of the user to be deleted
   * @throws ItemNotFoundException
   *           if there is no user with the given username
   * @throws SQLException
   *           if there was problem connecting to the sql database or there was
   *           a database error
   * @throws ClassNotFoundException
   *           if connector/j was not linked
   */
  public void deleteSession(Connection con, Session session) throws ItemNotFoundException, SQLException, ClassNotFoundException {
    Map<String, byte[]> conditions = new HashMap<String, byte[]>();
    conditions.put(SessionDBTScheme.COURSE_NAME.toString(), session.getCourse().getBytes());
    conditions.put(SessionDBTScheme.DAY.toString(), session.getDay().getBytes());
    conditions.put(SessionDBTScheme.START_TIME.toString(), Utility.castToString(session.getStart_time()).getBytes());
    conditions.put(SessionDBTScheme.END_TIME.toString(), Utility.castToString(session.getEnd_time()).getBytes());
    removeItem(con, conditions);
  }
  
  
  public void deleteAllSessions(Connection con,String course) throws ItemNotFoundException, SQLException, ClassNotFoundException {
    Map<String, byte[]> conditions = new HashMap<String, byte[]>();
    conditions.put(SessionDBTScheme.COURSE_NAME.toString(), course.getBytes());
    removeItem(con, conditions);
  }
  /**
   * Adds the given user to the database, if he does exist, throws an exception
   * 
   * @param u
   *          the user record to insert
   * @throws ItemAlreadyInDBException
   *           if there is already a user with the given username in the
   *           database
   * @throws SQLException
   *           if there was problem connecting to the sql database or there was
   *           a database error
   * @throws ClassNotFoundException
   *           if connector/j was not linked
   */
  public void insertSession(Connection con, Session session) throws ItemAlreadyInDBException, SQLException, ClassNotFoundException {
    Map<String, byte[]> record = new HashMap<String, byte[]>();
    record.put(SessionDBTScheme.COURSE_NAME.toString(),session.getCourse().getBytes());
    record.put(SessionDBTScheme.DAY.toString(),session.getDay().getBytes());
    record.put(SessionDBTScheme.START_TIME.toString(),Utility.castToString(session.getStart_time()).getBytes());
    record.put(SessionDBTScheme.END_TIME.toString(),Utility.castToString(session.getEnd_time()).getBytes());
    insertItem(con, record);
    //if (true) throw new InvalidParameterException("5");
  }
}
